package edu.umn.msse.farside.FourH.web;

/**
 * Created by IntelliJ IDEA.
 * User: corcutt
 * Date: Mar 17, 2009
 * Time: 6:27:15 PM
 * To change this template use File | Settings | File Templates.
 */

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SecurityFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;

        //String requestPath = httpRequest.getPathInfo()
        String requestUri = httpRequest.getRequestURI();
        if (requestUri != null && !requestUri.contains("/login") && !requestUri.contains("/createVolunteer")) {
            HttpSession session = httpRequest.getSession(false);   // false means don't create session if it doesn't exist
            if (session == null || session.getAttribute("volunteer") == null) {  // don't let them get to the site, redirect to login page
                ((HttpServletResponse)response).sendRedirect("/fourH/login");
                return;
            }
        }

        chain.doFilter(request, response);
        }

    public void destroy() {
    }
}
